<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/5
 * Time: 8:58
 */

namespace app\admin\model;


use think\Db;
use think\Loader;
use think\Model;

class Article extends Model
{
    protected $autoWriteTimestamp = true;
    protected $auto   = [];
    protected $insert = ['attr', 'photos', 'enclosure'];
    protected $update = [];

    protected $dateFormat = 'Y-m-d H:i';

    protected function setAttrAttr($value)
    {
        return empty($value) ? null : implode(',', $value);
    }

    protected function setPhotosAttr($value)
    {
        return json_encode($value);
    }

    protected function setEnclosureAttr($value)
    {
        return json_encode($value);
    }

    public function getAttrAttr($value)
    {
        return !empty($value) ? explode(',', $value) : '';
    }

    public function getPhotosAttr($value)
    {
        return json_decode($value, true);
    }

    public function getEnclosureAttr($value)
    {
        return json_decode($value, true);
    }

    public function add($data)
    {
        $this->data($data);
        $this->allowField(true)->isUpdate(false)->save();
        return $this;
    }

    public function edit($data)
    {
        unset($data['thumb_del']);
        unset($data['photos_del']);
        unset($data['enclosure_del']);
        $this->update($data);
        return $this;
    }
}